package github.aloofcoder.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import github.aloofcoder.gulimall.product.entity.ProductAttrValueEntity;
import github.aloofcoder.gulimall.product.service.ProductAttrValueService;
import github.aloofcoder.gulimall.product.vo.AttrGroupRelationVo;
import github.aloofcoder.gulimall.product.vo.AttrRespVo;
import github.aloofcoder.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import github.aloofcoder.gulimall.product.entity.AttrEntity;
import github.aloofcoder.gulimall.product.service.AttrService;
import github.aloofcoder.common.utils.PageUtils;
import github.aloofcoder.common.utils.R;



/**
 * 商品属性
 *
 * @author hanle
 * @email hanl1946@163.com
 * @date 2020-12-03 22:29:55
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * /product/attr
     * 列表
     */
    @RequestMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", data);
    }


    // /product/attr/base/list/{catelogId}
    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R baseAttrList(@PathVariable("catelogId") Long catelogId,
                          @PathVariable("type") String type,
                          @RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryBaseAttrList(catelogId, params, type);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }
//    /product/attr/update/{spuId}

    @RequestMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody List<ProductAttrValueEntity> entities, @PathVariable("spuId") Long spuId){
        productAttrValueService.updateSpuAttr(spuId, entities);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
}
